package com.gegao.learndemo.CustomView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gegao.learndemo.R;

/**
 * 标题栏
 */
public class TopBar extends RelativeLayout {

    private Context mContext;

    private Button mLeftButton;
    private Button mRightButton;
    private TextView mTitleView;

    private String mTitleText;
    private int mTitleTextColor;
    private float mTitleTextSize;

    private String mLeftText;
    private int mLeftTextColor;
    private float mLeftTextSize;
    private Drawable mLeftBackground;

    private String mRightText;
    private int mRightTextColor;
    private float mRightTextSize;
    private Drawable mRightBackground;

    private RelativeLayout.LayoutParams mTitleParams;
    private RelativeLayout.LayoutParams mLeftParams;
    private RelativeLayout.LayoutParams mRightParams;


    public TopBar(Context context) {
        this(context, null);
    }

    public TopBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttributes(attrs);
        initViews();
    }

    private void initAttributes(AttributeSet attrs) {
        //通过这个方法，将attr.xml中定义的declare-styleable
        //的所有属性的值储存到TypeArray中
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TopBar);

        //从TypeArray中取出相应的值类为要设置的属性赋值
        mTitleText = typedArray.getString(R.styleable.TopBar_titleText);
        mTitleTextColor = typedArray.getColor(R.styleable.TopBar_titleColor, 0);
        mTitleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 16);

        mLeftText = typedArray.getString(R.styleable.TopBar_leftText);
        mLeftTextColor = typedArray.getColor(R.styleable.TopBar_leftTextColor, 0);
        mLeftTextSize = typedArray.getDimension(R.styleable.TopBar_leftTextSize, 16);
        mLeftBackground = typedArray.getDrawable(R.styleable.TopBar_leftBackground);

        mRightText = typedArray.getString(R.styleable.TopBar_rightText);
        mRightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0);
        mRightTextSize = typedArray.getDimension(R.styleable.TopBar_rightTextSize, 16);
        mRightBackground = typedArray.getDrawable(R.styleable.TopBar_rightBackground);

        // 获取完typedArray的值一般都要调用
        // recycle 方法来避免重新创建的时候出错
        typedArray.recycle();

    }

    private void initViews() {
        mTitleView = new Button(mContext);
        mLeftButton = new Button(mContext);
        mRightButton = new Button(mContext);

        //为创建额组建赋值
        mTitleView.setText(mTitleText);
        mTitleView.setTextColor(mTitleTextColor);
        mTitleView.setTextSize(mTitleTextSize);

        mLeftButton.setText(mLeftText);
        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setTextSize(mLeftTextSize);
        mLeftButton.setBackground(mLeftBackground);

        mRightButton.setText(mRightText);
        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setTextSize(mRightTextSize);
        mRightButton.setBackground(mRightBackground);

        //为组建设置相应的布局元素
        mTitleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(mTitleView, mTitleParams);

        mLeftParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        addView(mLeftButton, mLeftParams);

        mRightParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(mRightButton, mRightParams);
    }
}
